<template>
  <div class="m-content">
    <h3>欢迎来到SpringBoot博客</h3>
    <div class="block">
      <el-avatar :size="50" :src="user.avatar"></el-avatar>
      <div>{{user.username}}</div>
    </div>
    <div class="maction">
      <span>
        <el-link href="/blogs">主页</el-link>
      </span>
      <el-divider direction="vertical"></el-divider>
      <span>
        <el-link type="success" href="/blog/add">发表文章</el-link>
      </span>
      <el-divider direction="vertical"></el-divider>
      <span v-show="!hasLogin">
        <el-link type="primary" href="/login">登录</el-link>
        <el-divider direction="vertical"></el-divider>
      </span>
      <span v-show="hasLogin">
        <el-link type="danger" @click="logout">退出</el-link>
        <el-divider direction="vertical"></el-divider>
      </span>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      user: {
        username: "请先登录",
        avatar: "",
      },
      hasLogin:false
    };
  },
  created() {
      this.getUser();
  },
  methods: {
    getUser() {
      if(this.$store.getters.getUser){
          var userInfo = this.$store.getters.getUser;
          this.user.username = userInfo.username;
          this.user.avatar = userInfo.avatar;
          this.hasLogin = true;
      }
    },
    logout() {
      var vm = this;
      vm.$axios({
        method: "Get",
        url: "/logout",
        headers: {
            "token": localStorage.getItem("token"),
        },
      }).then((res) => {
        vm.hasLogin = false;
        vm.$store.commit("REMOVE_INFO");
        vm.$router.push("/login");
      });
    },
  },
};
</script>
<style scoped>
.m-content {
  max-width: 960px;
  margin: 0 auto;
  text-align: center;
}
.maction {
  margin: 10px 0;
}
</style>